<?php
namespace app\controller;

use think\Request;
use think\facade\Db;

class Menu extends Common
{
	/**
	 * 菜单数据
	 *
	 * @return
	 */
	public function getMenu(Request $request)
	{
		$array = [];
		switch ($request->sessInfo['level']) {
			case 1:
				$array = Db::table('cfg_menu')->where('parentId', 0)->order('sortNumber', 'asc')->select()->toArray();
				foreach($array as &$value){
					$value['children'] = Db::table('cfg_menu')->where('parentId', $value['menuId'])->order('sortNumber', 'asc')->select()->toArray();
				}
				break;

			case 2:
				$array = Db::table('cfg_menu')->where('parentId', 0)->where('menuLevel', 0)->order('sortNumber', 'asc')->select()->toArray();
				foreach($array as &$value){
					$value['children'] = Db::table('cfg_menu')->where('menuLevel', 0)
					  ->where('parentId', $value['menuId'])->order('sortNumber', 'asc')->select()->toArray();
				}
				break;

			case 3:
				$userRole = Db::table('cfg_user')->where('id', $request->sessInfo['id'])->value('role');
				$roleValue = Db::table('cfg_role')->where('id', $userRole)->value('roleValue');
				$array = Db::table('cfg_menu')->where('parentId', 0)->where('menuLevel', 0)->where('menuId', 'in', $roleValue)->order('sortNumber', 'asc')->select()->toArray();
				foreach($array as &$value) {
					$value['children'] = Db::table('cfg_menu')->where('menuLevel', 0)
					  ->where('parentId', $value['menuId'])
						->where('menuId', 'in', $roleValue)->order('sortNumber', 'asc')->select()->toArray();
				}
				break;
		}
		return json(['msg'=>'操作成功', 'code'=>0, 'data' => $array]);
	}

}
